package com.zhou.syt.hosp.controller;

import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.zhou.syt.common.result.Result;
import com.zhou.syt.hosp.service.HospitalSetService;
import com.zhou.syt.util.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @ClassName: HospitalSetController
 * @Description:
 * @Author: admin
 * @Date: 2021/12/11 13:23
 */
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@Api("医院配置管理")
@CrossOrigin
public class HospitalSetController {

    @Resource
    private HospitalSetService hospitalSetService;

    @ApiOperation("医院表信息")
    @GetMapping("/list")
    public Result list() {
        List<HospitalSet> list = hospitalSetService.list();

        return Result.ok(list);
    }

    @ApiOperation("删除医院配置")
    @DeleteMapping("/{id}")
    public Result removeHospSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag == true) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //分页查询
    @PostMapping("findPage/{current}/{limit}")
    @ApiOperation("分页查询医院配置")
    public Result findPage(@PathVariable long current,
                           @PathVariable long limit,
                           @RequestBody(required = false) HospitalQueryVo hospitalQueryVo) {
        //创建配置对象，传递当前页，每一页记录
        Page<HospitalSet> hospitalSetPage = new Page<HospitalSet>(current, limit);

        //构建条件
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();

        //判断是否有值
        String hoscode = hospitalQueryVo.getHoscode();
        String hosname = hospitalQueryVo.getHosname();
        if (!StringUtils.isNullOrEmpty(hoscode)) {
            queryWrapper.eq("hoscode", hospitalQueryVo.getHoscode());
        }

        if (!StringUtils.isNullOrEmpty(hosname)) {
            queryWrapper.like("hosname", hospitalQueryVo.getHosname());
        }

        //调用方法实现查询
        Page<HospitalSet> page = hospitalSetService.page(hospitalSetPage, queryWrapper);
        return Result.ok(page);
    }

    //添加医院设置
    @PostMapping("saveHosp")
    @ApiOperation("添加医院设置")
    public Result savaHosp(@RequestBody HospitalSet hospitalSet) {
        //设置状态，0为不可用
        hospitalSet.setStatus(1);

        Random random = new Random();
        //设置签名
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));

        boolean save = hospitalSetService.save(hospitalSet);
        if (save == false) {
            return Result.fail();
        }

        return Result.ok();
    }

    //根据id获取医院设置
    @GetMapping("/getHos/{id}")
    @ApiOperation("根据id获取信息")
    public Result getHos(@PathVariable Long id) {
        HospitalSet byId = hospitalSetService.getById(id);
        if (byId == null) {
            return Result.fail();
        }

        return Result.ok(byId);
    }

    //修改医院
    @PostMapping("/upateHos")
    @ApiOperation("修改医院信息")
    public Result updateHos(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag == false) {
            return Result.fail();
        }

        return Result.ok();
    }

    //批量删除
    @DeleteMapping("/batchDelete")
    @ApiOperation("批量删除")
    public Result batchDeleteHos(@RequestBody List<Long> ids) {
        boolean b = hospitalSetService.removeByIds(ids);
        if (b == false) {
            return Result.fail();
        }
        return Result.ok();
    }

    //设置医院加锁和解锁
    @PutMapping("/lockHos/{id}/{status}")
    @ApiOperation("医院加锁解锁")
    public Result lockHos(@PathVariable Long id,
                          @PathVariable Integer status) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if (hospitalSet == null) {
            return Result.fail();
        }

        hospitalSet.setStatus(status);
        boolean flag = hospitalSetService.updateById(hospitalSet);

        if (flag == false) {
            return Result.fail();
        }

        return Result.ok();

    }
}
